using System;
using System.Collections.Generic;
using NUnit.Framework.Internal;
using UnityEditor;
using UnityEngine;

namespace Gj
{
    [CustomPropertyDrawer(typeof(SelectArea))]
    public class SelectAreaDrawer: PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
        {
            var value = property.intValue;
            var areaIndex = -1;
            var areaNames = GameObjectUtility.GetNavMeshAreaNames();
            for (var i = 0; i < areaNames.Length; i++)
            {
                var areaValue = GameObjectUtility.GetNavMeshAreaFromName(areaNames[i]);
                if (areaValue == value)
                    areaIndex = i;
            }
            
            EditorGUI.BeginChangeCheck();
            EditorGUI.BeginProperty(position, GUIContent.none, property);
            areaIndex = EditorGUI.Popup(position, content.text, areaIndex, areaNames);
            if (EditorGUI.EndChangeCheck())
            {
                property.intValue = areaIndex;
            }

            EditorGUI.EndProperty();
        } 
    }
}